package com.jiyun.service.impl;
/*
 * @Author:limengyao
 * @Date:2023-08-22 15:18
 * @Filename:UserServiceImpl
 *
 */


import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.Result;
import com.jiyun.ResultClasss;
import com.jiyun.fegin.TestService;
import com.jiyun.mapper.UserMapper;
import com.jiyun.pojo.day01.Member;
import com.jiyun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,Member> implements UserService {

    @Autowired
    private TestService testService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ResultClasss userlogin(Member member) {
        try {
            ResultClasss resultClasss = JSON.parseObject(testService.memberlogin(member), ResultClasss.class);
            if(resultClasss.getCode()==2) return  resultClasss;
            Object data = resultClasss.getData();
            Map<String,Object> map=new HashMap<>();
            map.put("token",data);
            String token = JWTUtil.createToken(map, "123".getBytes());
            redisTemplate.opsForHash().put("用户信息",token,JSON.toJSONString(data));
            return ResultClasss.ok(token).Message("登录成功");
        }catch (Exception e){
            e.printStackTrace();
            return  ResultClasss.error().Message("登录失败");
        }
    }
}
